
(function () {
    Codestrong.ui.createLectureWindow = function () {
    	Ti.App.Properties.setInt('isDownload', 0);
      
        if(Codestrong.isAndroid()) Titanium.include('../globals.js');
        else 	 Titanium.include('globals.js');
        var fontsizeDefault,fontsizeLarge,topTableView,leftTableView;
        var btwidth=200;
		var btheight=45;
		var spacebutton=1,sizebutton=150,sizeimage=200
		var height_btIncludeImage=40;
	      if(Ti.Platform.osname == 'iphone' )
		  {
		    Ti.App.Properties.setString('osname', 'iphone');
		  	Ti.App.Properties.setString('fontsizeDefault', Globals.fontsizeDefault_iphone);
		  	Ti.App.Properties.setString('fontsizeLarge', Globals.fontsizeLarge_iphone);
		  	Ti.App.Properties.setString('topTableView', 29);
		  	Ti.App.Properties.setString('leftTableView', 1);
		    btwidth=100;
		    btheight=27;
		    height_btIncludeImage=29;
		  }
		  else
		  {
		  	Ti.App.Properties.setString('osname', 'noniphone');
		  	Ti.App.Properties.setString('fontsizeDefault', Globals.fontsizeDefault_noniphone);
		  	Ti.App.Properties.setString('fontsizeLarge', Globals.fontsizeLarge_noniphone);
		    Ti.App.Properties.setString('topTableView', 250);
		   	Ti.App.Properties.setString('leftTableView', 5);
		   	btwidth=200;
		    btheight=45;
		   	height_btIncludeImage=50;
		  }	
    
        var win = Titanium.UI.createWindow({
            id: 'lectureWindow',
            title: 'BÀI GIẢNG',
            backgroundColor: '#FFF',
            barColor: '#414444',
            fullscreen: false
           
        });
        //////////////////////////EVEN FORM///////////////////////
        Ti.App.addEventListener('removecontrol_TableViewPanel', function() {
        	win.remove(TableViewSeeMorePanel)
        })
         Ti.App.addEventListener('addcontrol_TableViewPanel', function() {
        	////////////TABLE SCROOL VIEW///////////////////////////
			
			var TableViewSeeMorePanel=require('windows/LectureWindow_TableView').CreateLectureWindow_TableView({
			  			 heightview:'100%',
						 widthview:Globals.platformWidth,
						 leftview:0,
			  			 topview:scrollViewImage.top+scrollViewImage.height,
			  			 infolabel:'Danh sách bài giảng xem nhiều',
			  			 urljson:Globals.serviceurl_GetListLectureTopNew,
			  			
				    })
			
			win.add(TableViewSeeMorePanel);
			///////////////////////////////////////////////////////////
        })
        Ti.App.addEventListener('ListVideoWindow:btDownload', function() {
			Ti.App.Properties.setInt('isDownload', 1);
			getListLectureDownload(tableview);
		});
		Ti.App.addEventListener('ListVideoWindow:btLectureSee', function() {
			// if (Ti.Network.online) {
			 		Ti.App.Properties.setInt('isDownload', 0);
					//Codestrong.ui.activityIndicator.showModal(Globals.loading, Globals.timeout, Globals.loadingTimeout);
					//getListLectureTopNew(tableview);
					Ti.App.fireEvent('LectureSeeMore_SetData'); 
			 //}
			 //else
			///  {
			  	   // createAlertConnectError()
			//  }
		});
    	/////////////BUTTON VIEW///////////////////////////
		var scrollViewbutton = Titanium.UI.createScrollView({
				contentWidth:'auto',
				contentHeight:'auto',
				top:0,
				width:'100%',
				height:45,
				backgroundColor:'white',
				//backgroundImage:Globals.backgroundImage_PanelImageView,
				scrollType: 'horizontal',
				
				
		});	
		
		
		var btSearch=require('windows/LectureWindow_ButtonView').CustomButton({
				  			title:Globals.btSearch,
				  			width:sizebutton,
				  			even:'ListVideoWindow:btSearch',
				  			left:0
					    })
		scrollViewbutton.add(btSearch);
		
		var btLectureSee=require('windows/LectureWindow_ButtonView').CustomButton({
				  			title:Globals.btLectureSeeMore,
				  			width:sizebutton,
				  			even:'ListVideoWindow:btLectureSee',
				  			left:sizebutton+spacebutton,
				  			isSelect:1
					    })
		scrollViewbutton.add(btLectureSee);
		
		var btTopic=require('windows/LectureWindow_ButtonView').CustomButton({
				  			title:'Chủ đề',
				  			width:sizebutton,
				  			even:'ListVideoWindow:btTopic',
				  			left:2*(sizebutton+spacebutton)
					    })
		scrollViewbutton.add(btTopic);
		var btTeacher=require('windows/LectureWindow_ButtonView').CustomButton({
				  			title:'Giáo viên',
				  			width:sizebutton,
				  			even:'ListVideoWindow:btTeacher',
				  			left:3*(sizebutton+spacebutton)
					    })
		scrollViewbutton.add(btTeacher);
		var btLike=require('windows/LectureWindow_ButtonView').CustomButton({
				  			title:'Yêu thích',
				  			width:sizebutton,
				  			even:'ListVideoWindow:btLike',
				  			left:4*(sizebutton+spacebutton)
					    })
		scrollViewbutton.add(btLike);
		var btDownload=require('windows/LectureWindow_ButtonView').CustomButton({
				  			title:Globals.btLectureSee,
				  			width:sizebutton,
				  			even:'ListVideoWindow:btDownload',
				  			left:5*(sizebutton+spacebutton)
					    })
		scrollViewbutton.add(btDownload);
		win.add(scrollViewbutton);
		
		
		
      
      ////////////////PANEL//////////////
      /*
	 	var picturepanel=require('windows/LectureWindow_HeaderView').LectureWindow_HeaderView({
		  			 height:45,
		  			 top:scrollView.height,
		  			 info:'Tin nổi bật'
			    })
		win.add(picturepanel);
	*/

       /////////////IMAGE VIEW///////////////////////////
		var scrollViewImage
		if(Ti.App.Properties.getString('osname')!='iphone')
		{
		   scrollViewImage = Titanium.UI.createScrollView({
					contentWidth:'auto',
					contentHeight:'auto',
					top:scrollViewbutton.height+scrollViewbutton.top,
					width:'100%',
					height:200,
					//backgroundColor:'white',
					//backgroundImage:'images/tv.png',
					scrollType: 'horizontal',
					
					
			});	
			var mobilier = [];
			mobilier[0] = 'http://thaytro.vn/resource/upload/teacher/slide_8_19112008.jpg';
			mobilier[1] = 'http://thaytro.vn/resource/upload/teacher/slide_12.jpg';
			mobilier[2] = 'http://thaytro.vn/resource/upload/teacher/slide_24.jpg';
			mobilier[3] = 'http://thaytro.vn/resource/upload/user/slide_23.jpg';
			mobilier[4] = 'http://thaytro.vn/resource/upload/teacher/slide_10_20112008.jpg';
			mobilier[5] = 'http://thaytro.vn/resource/upload/teacher/slide_16.jpg';
			mobilier[6] = 'http://thaytro.vn/resource/upload/user/slide_9_20112008.jpg';
			
			
			for(var i = 0; i < mobilier.length; i++) {
				
					var image=require('windows/LectureWindow_ImageView').CreateLectureWindow_ImageView({
				  			//settings:settings,
					        top:0,
					        left:i*(sizeimage+spacebutton),
					        height:200,
					        width:200,
					        image:mobilier[i],
					        info:'Thông tin mẩu '+i
					 })
					scrollViewImage.add(image)
			}
			
			win.add(scrollViewImage)
		}

	
		////////////TABLE SCROOL VIEW///////////////////////////
	
		var TableViewSeeMorePanel=require('windows/LectureWindow_TableView').CreateLectureWindow_TableView({
		  			 heightview:'100%',
					 widthview:Globals.platformWidth,
					 leftview:0,
		  			 topview:scrollViewImage.top+scrollViewImage.height,
		  			 infolabel:'Danh sách bài giảng xem nhiều',
		  			 urljson:Globals.serviceurl_GetListLectureTopNew,
			    })
	
		win.add(TableViewSeeMorePanel);
		///////////////////////////////////////////////////////////
		
		var getTableData = function(LectureId,TypeID) {
			var response
			var row = null;
			var data = [];
			var param = {        
			  "LectureId" : LectureId,
			  "TypeID" : TypeID
			};
			var xhr = Titanium.Network.createHTTPClient({
		   
				   onload : function(e) 
				   {
				       //Titanium.API.info(this.responseText);
				       
				       //do whatever you want to do with your response
				   
				
						 json = this.responseText;
						 json=json.replace('<string xmlns="http://tempuri.org/">', '');
						 json=json.replace('<?xml version="1.0" encoding="utf-8"?>', '');
						 json=json.replace('</string>', '');
						//  alert(json)
						 response = JSON.parse(json);
						 for (i = 0; i < response.OBJECT.length; i++) {
							olecture = response.OBJECT[i];
							//	alert(fighter.Name)
							row = Ti.UI.createTableViewRow({
								height:'50dp',
							    LectureId: olecture.LectureId,
					        	VideoName:  olecture.VideoName,
					        	VideoFile:  olecture.VideoFile,
					        	VideoID:olecture.Id,
					        	VideoFile:  olecture.VideoFile,
							});
						    ImageTeacher = Ti.UI.createImageView({
						            backgroundImage:'images/video45.png',//olecture.TeacherImage,//imageUrl[i],
						          //  top:'5dp',
						            width:'45dp',
						            left: '5dp',
						            height:'45dp',
						           // borderColor: '#fff',
						           // borderWidth:'2dp',
						           // borderRadius:'7dp'
						            
						        });
							//alert(olecture.VideoName)
							LectureName = Ti.UI.createLabel({
								text:olecture.VideoName,
								font:{
									fontSize:Ti.App.Properties.getString('fontsizeDefault')//,
									//fontWeight:'bold'
								},
								height:'auto',
								left:'60dp',
								right:10,//'68dp',
								//top:'5dp',
								color:Globals.color_TableView,
								touchEnabled:false
							});
							
						    row.add(ImageTeacher);
							row.add(LectureName);
							data.push(row);
						}
						//tableview.setData(data);
						Codestrong.ui.activityIndicator.hideModal();
				   }, 
		   
				   onerror : function(e) {
				   	 
				       Codestrong.ui.activityIndicator.hideModal();
				   },
				   timeout : Globals.timeout //5000
		    
			});
			xhr.open("POST",Globals.serviceurl_GetListVideo);
			xhr.send(param);
			return data;
		};		
		
		
		
		
		
		
		
		/*
		
		var picturepanellecture=require('windows/LectureWindow_HeaderView').LectureWindow_HeaderView({
		  			 height:45,
		  			 top:scrollViewImage.top+scrollViewImage.height,
		  			 info:'Danh sách bài giảng'
			    })
		win.add(picturepanellecture);


       
       
       var tableview = Ti.UI.createTableView({
			top:picturepanellecture.top+picturepanellecture.height,
			
		    backgroundColor:'transparent',//Globals.backgroundColor_TableView,
			backgroundImage:Globals.backgroundImage_TableView,
			
		     
		});
       
       win.add(tableview);
	   tableview.addEventListener('click', function(e)
		{
			
			//Titanium.API.info(e);
			Ti.App.Properties.setInt('LectureID', e.rowData.Id);
			var JSONLectureData= getTableData(e.rowData.Id,e.rowData.TypeID)
			//alert(JSONLecture)
			if(Ti.App.Properties.getInt('isDownload')==1)
			{
		    	 Codestrong.navGroup.open(Codestrong.ui.createListVideoDownloadWindow({
		         Id: e.rowData.Id,
		         IdMon:  e.rowData.IdMon,
		         IdLop:  e.rowData.IdLop,
		         TypeID:e.rowData.TypeID,
		         LectureName: e.rowData.Name,
		         Detail: e.rowData.Detail,
		         JSONLectureData:JSONLectureData
			    }), 
			    {
			        animated: true
			    });
			}
			else
			{
				
		    	 Codestrong.navGroup.open(Codestrong.ui.createListVideoWindow({
		         Id: e.rowData.Id,
		         IdMon:  e.rowData.IdMon,
		         IdLop:  e.rowData.IdLop,
		         TypeID:e.rowData.TypeID,
		         LectureName: e.rowData.Name,
		         Detail: e.rowData.Detail,
		         JSONLectureData:JSONLectureData
			    }), 
			    {
			        animated: true
			    });
			}
			
			
		});
		
		
		
							
		
		*/

/*
var entryView = Ti.UI.createView({
  backgroundImage:Globals.backgroundImage_PanelImageView,
  backgroundColor:Globals.barColor,//'#0060AA',
  width:'100%',
  height:height_btIncludeImage,
  top:0
});

var controlsView = Ti.UI.createView({
 // width:270,
  height:'auto'
});

var btSearch = Titanium.UI.createButton({
	title:Globals.btSearch,
	font:{
							fontSize:Ti.App.Properties.getString('fontsizeDefault')//,
							//fontWeight:'bold'
						},
	backgroundImage:Globals.backgroundImage_button,
	backgroundSelectedImage:Globals.backgroundSelectedImage,
	backgroundDisabledImage: Globals.backgroundDisabledImage,
	width:btwidth,
	height:btheight,
	left:1,
	color:Globals.foreColor
	
	
});
var btTopNew= Titanium.UI.createButton({
	title:Globals.btLectureSeeMore,
	font:{
							fontSize:Ti.App.Properties.getString('fontsizeDefault')//,
							//fontWeight:'bold'
						},
	backgroundImage:Globals.backgroundImage_buttonSelect,
	backgroundSelectedImage:Globals.backgroundSelectedImage,
	backgroundDisabledImage: Globals.backgroundDisabledImage,
	width:btwidth,
	height:btheight,
	center:0,
	color:Globals.foreColor
	
});
var btDownload = Titanium.UI.createButton({
	title:Globals.btLectureSee,
	font:{
							fontSize:Ti.App.Properties.getString('fontsizeDefault')//,
							//fontWeight:'bold'
						},
	backgroundImage:Globals.backgroundImage_button,
	backgroundSelectedImage:Globals.backgroundSelectedImage,
	backgroundDisabledImage: Globals.backgroundDisabledImage,
	width:btwidth,
	height:btheight,
	right:1,
	color:Globals.foreColor
});
controlsView.add(btSearch);
controlsView.add(btTopNew);
controlsView.add(btDownload);
entryView.add(controlsView);
win.add(entryView);


var ff=Ti.UI.createView({
	height:300,width:300
})
var gg=Ti.UI.createButton({
	title:'TEST XEM'
})
ff.add(gg)

var createAlertConnectError = function()
{
	var confirm = Titanium.UI.createAlertDialog({ 
					            title: Globals.title1, 
					            message:Globals.connecterror, 
					            buttonNames: [Globals.btOk]
					    });
	confirm.show();   
}

btSearch.addEventListener('click',function()
{
	
	  if (Ti.Network.online) {
	  	Ti.App.Properties.setInt('isDownload', 0);
	  	btSearch.backgroundImage=Globals.backgroundImage_buttonSelect;
		btTopNew.backgroundImage=Globals.backgroundImage_button;
		btDownload.backgroundImage=Globals.backgroundImage_button;
		
		//btSearch.enabled=btLectureNew.enabled= btLectureSee.enabled=false;
		
		addPopupWindow(win);
	  }
	  else
	  {
	  	    createAlertConnectError()
	  }
	
})

btTopNew.addEventListener('click',function()
{
	 if (Ti.Network.online) {
	 		Ti.App.Properties.setInt('isDownload', 0);
			btSearch.backgroundImage=Globals.backgroundImage_button;
			btTopNew.backgroundImage=Globals.backgroundImage_buttonSelect;
			btDownload.backgroundImage=Globals.backgroundImage_button;
			
			Codestrong.ui.activityIndicator.showModal(Globals.loading, Globals.timeout, Globals.loadingTimeout);
			getListLectureTopNew(tableview);
	 }
	 else
	  {
	  	    createAlertConnectError()
	  }
})
	
btDownload.addEventListener('click',function()
{
	Ti.App.Properties.setInt('isDownload', 1);
	btSearch.backgroundImage=Globals.backgroundImage_button;
	btTopNew.backgroundImage=Globals.backgroundImage_button;
	btDownload.backgroundImage=Globals.backgroundImage_buttonSelect;
	getListLectureDownload(tableview);
	
	
	
	
	
})
*/

win.addEventListener('focus', function()
{
	
});

var _createTableViewRow = function(olecture) {
	
	row = Ti.UI.createTableViewRow({
						height:90,
						Id: olecture.Id,
						IdMon: olecture.IdMon,
						IdLop: olecture.IdLop,
						Name: olecture.Name,
						TypeID:olecture.IdLop,
						Detail: olecture.ChuyenDeName+' - '+olecture.ChuongName+' , ' + olecture.TeacherName
					});
	return row;
}

var _createTableViewRowDownload = function(olecture) {
	
	row = Ti.UI.createTableViewRow({
						height:'90dp',
						Id: olecture.Id,
						IdMon: olecture.IdMon,
						IdLop: olecture.IdLop,
						Name: olecture.Name,
						TypeID:olecture.IdLop,
						Detail: olecture.Detail
					});
	return row;
}
/////////////EVENT///////////////////////////
var getListLecture = function(tableview,IDMon,SearchName) {
	//activityIndicator.show();
	var row = null;
	var data = [];
	var param = {        
	  "IDMon" : IDMon,
	  "SearchName": SearchName
	};
	try {
	   		    	var xhr = Titanium.Network.createHTTPClient({
   
		   onload : function(e) 
		   {
	   		    
		   	
		   	
		       Titanium.API.info(this.responseText);
		       
		       //do whatever you want to do with your response
		   
		       
				 json = this.responseText;
				
				 json=json.replace('<string xmlns="http://tempuri.org/">', '');
				 json=json.replace('<?xml version="1.0" encoding="utf-8"?>', '');
				 json=json.replace('</string>', '');
				//  alert(json)
				 response = JSON.parse(json);
			//	alert(response.OBJECT.length);
				
		
				 
			 	 for (i = 0; i < response.OBJECT.length; i++) {
						olecture = response.OBJECT[i];
						//alert(olecture.IdMon)
					row = _createTableViewRow(olecture);
					//alert(olecture.TeacherImage)
					ImageTeacher = Ti.UI.createImageView({
				            backgroundImage:'images/video.png',//olecture.TeacherImage,//imageUrl[i],
				            //top:'5dp',
				            width:'80dp',
				            left: Ti.App.Properties.getString('leftTableView'),
				            height:'80dp',
				            //borderColor: '#fff',
				           // borderWidth:'2dp',
				           // borderRadius:'7dp'
				            
				        });
					
					LectureName = Ti.UI.createLabel({
						text:olecture.Name,
						font:{
							fontSize:Ti.App.Properties.getString('fontsizeDefault'),
							fontWeight:'bold'
						},
						height:'auto',
						left:'90dp',
						top:'5dp',
						right: Ti.App.Properties.getString('leftTableView'),
						
						color:Globals.color_TableView,
						touchEnabled:false
					});
					TeacherName = Ti.UI.createLabel({
						text:''+olecture.ChuyenDeName+' - '+olecture.ChuongName+' , ' + olecture.TeacherName + '',
						font:{
							fontSize:Ti.App.Properties.getString('fontsizeDefault')
						},
						height:'auto',
						left:'90dp',
						right: Ti.App.Properties.getString('leftTableView'),
					
						bottom:'5dp',
						color:Globals.color_TableView,
						touchEnabled:false
					});
					
					
				    
				 
					
					
				    row.add(ImageTeacher);
					row.add(LectureName);
					row.add(TeacherName);
				
					data.push(row);
				}
			
				tableview.setData(data);
				Codestrong.ui.activityIndicator.hideModal();
				//activityIndicator.hide();
				
		   }, 
   
		   onerror : function(e) {
		   	 Codestrong.ui.activityIndicator.hideModal();
		   	   //activityIndicator.hide();
		      // require('Dialog').showError();
		   },
		   timeout : Globals.timeout //5000
    
	});
	xhr.open("POST",Globals.serviceurl_GetListLecture);// "http://lhs.com.vn/JSONP-EndPoint.asmx/GetListLecture");
	xhr.send(param);
	//return data;
	   		    
		   	 	} catch (e) {
                        Ti.API.info(e);
                    }
	
};


/////////////EVENT///////////////////////////
var getListLectureTopNew = function(tableview) {
	//activityIndicator.show();
	var row = null;
	var data = [];
	var param = {        
	  "ID" : 1
	};
	var xhr = Titanium.Network.createHTTPClient({
   
		   onload : function(e) 
		   {
		       Titanium.API.info(this.responseText);
		       
		       //do whatever you want to do with your response
		   
		
				 json = this.responseText;
				 json=json.replace('<string xmlns="http://tempuri.org/">', '');
				 json=json.replace('<?xml version="1.0" encoding="utf-8"?>', '');
				 json=json.replace('</string>', '');
				//  alert(json)
				 response = JSON.parse(json);
				//alert(response.OBJECT.length);
				
		
				 
			 	 for (i = 0; i < response.OBJECT.length; i++) {
						olecture = response.OBJECT[i];
					//	alert(fighter.Name)
					row = _createTableViewRow(olecture);
					//alert(olecture.TeacherImage)
					ImageTeacher = Ti.UI.createImageView({
				            backgroundImage:'images/video.png',//olecture.TeacherImage,//imageUrl[i],
				            top:5,
				            width:80,
				            left: Ti.App.Properties.getString('leftTableView'),
				            height:80,
				          //  borderColor: '#fff',
				          //  borderWidth:'2dp',
				           // borderRadius:'7dp'
				            
				        });
					
					LectureName = Ti.UI.createLabel({
						text:olecture.Name,
						font:{
							fontSize:Ti.App.Properties.getString('fontsizeDefault'),
							fontWeight:'bold'
						},
						height:'auto',
						left:90,
						right: Ti.App.Properties.getString('leftTableView'),
						top:5,
						color:Globals.color_TableView,
						touchEnabled:false
					});
					TeacherName = Ti.UI.createLabel({
						text:''+olecture.ChuyenDeName+' - '+olecture.ChuongName+' , ' + olecture.TeacherName + '',
					    font:{
							fontSize:Ti.App.Properties.getString('fontsizeDefault')//,
							//fontWeight:'bold'
						},
						
						height:'auto',
						left:90,
						right: Ti.App.Properties.getString('leftTableView'),
						bottom:5,
						color:Globals.color_TableView,
						touchEnabled:false
					});
					
					
				  
					
					
				    row.add(ImageTeacher);
					row.add(LectureName);
					row.add(TeacherName);
					
					data.push(row);
				}
			
				tableview.setData(data);
				 Codestrong.ui.activityIndicator.hideModal();
				//activityIndicator.hide();
				
		   }, 
   
		   onerror : function(e) {
		   	  // activityIndicator.hide();
		      // require('Dialog').showError();
		      Codestrong.ui.activityIndicator.hideModal();
		      alert('ERROR')
		   },
		   timeout : Globals.timeout //5000
    
	});
	xhr.open("POST",Globals.serviceurl_GetListLectureTopNew);// "http://lhs.com.vn/JSONP-EndPoint.asmx/getListLectureTopNew");
	xhr.send(param);
	//return data;
};

/////////////EVENT///////////////////////////
var getListLectureDownload = function(tableview) {
	
	var db = require('db');
	var data = [];
	var row = null;
	var Items = db.selectLectureDownload();
	//alert(Items.length)
	for (var i = 0; i < Items.length; i++) {
		//alert(Items[i])
		row = _createTableViewRowDownload(Items[i]);
		ImageTeacher = Ti.UI.createImageView({
            backgroundImage:'images/video.png',//olecture.TeacherImage,//imageUrl[i],
            top:'5dp',
            width:'80dp',
            left: Ti.App.Properties.getString('leftTableView'),
            height:'80dp',
           // borderColor: '#fff',
           // borderWidth:'2dp',
           // borderRadius:'7dp'
            
        });
					
		LectureName = Ti.UI.createLabel({
			text:Items[i].Name,
			font:{
				fontSize:Ti.App.Properties.getString('fontsizeDefault'),
				fontWeight:'bold'
			},
			height:'auto',
			left:'90dp',
			top:'5dp',
			color:Globals.color_TableView,
			touchEnabled:false
		});
		TeacherName = Ti.UI.createLabel({
			text:Items[i].Detail,
			font:{
				fontSize:Ti.App.Properties.getString('fontsizeDefault')
			},
			height:'auto',
			left:'90dp',
			bottom:'5dp',
			color:Globals.color_TableView,
			touchEnabled:false
		});
	    row.add(ImageTeacher);
		row.add(LectureName);
		row.add(TeacherName);
		data.push(row);
	}
	tableview.setData(data);
	
};






	var firstRun = true;
	
	var reload= function () {
		
  		//Codestrong.ui.activityIndicator.showModal(Globals.loading, Globals.timeout, Globals.loadingTimeout);
		//if(btTopNew.backgroundImage==Globals.backgroundImage_buttonSelect)
		 //	getListLectureTopNew(tableview);
		//if(btDownload.backgroundImage==Globals.backgroundImage_buttonSelect)
		// 	getListLectureDownload(tableview);
		//else
		//	getListLectureTopNew(tableview);
        
        
        //var ScrollViewParent=scrollViewbutton.getParent();
        Titanium.API.info(TableViewSeeMorePanel.children[1])
        
		var length=scrollViewbutton.children.length;
	
		for (i = 0; i < length; i++) {
		   //get View
           var viewparent =scrollViewbutton.children[i];
           viewparent.backgroundImage='images/btOn.png';
        
		  //Check view is select
		  if(viewparent.backgroundImage==Globals.btSelectImage)
		  {
		  	  ////////////GET LABEL SELECT///////
           		var labelparent =viewparent.children[0];
          	 	alert(labelparent.text)
			  switch (labelparent.text) {
			  
		          case Globals.btLectureSeeMore: {
		          		
						  }
		          break;
		          case 1:{ 
		          	}
		          break;
		 
		          default:
		          break;
		      }
		  }
		  
		  
        }
        
        
    };
    
 // Get the tweets for 'twitter_name'
        if (Ti.Network.online) {
            win.addEventListener('open', function (e) {
                if (firstRun) {
                	
                    firstRun = false;
                    //reload();
                   // Ti.App.fireEvent('LectureSeeMore_SetData');
                }
            });
			/*
            if (Codestrong.isAndroid()) {
                win.activity.onCreateOptionsMenu = function (e) {
                    var menuitem = e.menu.add({
                        title: 'Refresh Tweets'
                    });
                    menuitem.addEventListener('click', function (e) {
                        reloadAllTweets();
                    });
                };
            } else {
                var button = Ti.UI.createButton({
                    systemButton: Ti.UI.iPhone.SystemButton.REFRESH
                });
                button.addEventListener('click', function (e) {
                    reloadAllTweets();
                });
                win.rightNavButton = button;
            }
            */
        } else {
           // alert(Globals.connecterror);
        }
/////////////////////////FUNCTION //////////////////////////////////////////////////////////////

/////////////////////////LOADING///////////////////////////////////////////////////////////
//get View
//var tableparent =scrollViewbutton.children[i];
//Codestrong.ui.activityIndicator.showModal(Globals.loading, Globals.timeout, Globals.loadingTimeout);
//getTableData_SeeMore(tableparent)

///////////////////////////////POPUP SEARCH////////////////////////////////////////////////
var pickerMonHoc,pickerLop;
function addPopupWindow( win1 ) {
 
    if ( win1.popUp != undefined ) {
        win1.remove(win1.popUp);
    }
 
    var popup_view = Titanium.UI.createView({
        width: Titanium.Platform.displayCaps.platformWidth,//'100%',
        //backgroundColor:'#999999',
        backgroundImage:Globals.backgroundImage_PanelImageView,
        borderColor:'#333333',
        top:0,
        left:0,
       // bottom:-240,
        height:210
    });
   
 
  
 
    ///////////////////////////////SEARCH///////////////////////////////////////////////////
 	var lbSearch = Ti.UI.createLabel({
		text:'Tên bài giảng',
		font:{
			fontSize:Ti.App.Properties.getString('fontsizeDefault')
		},
		
		height:'auto',
		left:10,
		top:'15dp',
		color:Globals.color_TableView
		
	});
	popup_view.add(lbSearch);
    var textSearch= Ti.UI.createTextField({
    	
        hintText: 'Nhập tên bài giảng',
        keyboardType: Ti.UI.KEYBOARD_DEFAULT,
        font:{
        	fontSize:Ti.App.Properties.getString('fontsizeDefault')//,fontWeight : 'bold'
        	},
        borderColor:'#666',
        borderWidth:1,
        top:'5dp',
        //left:50,
        right:10, 
        height:45,
        width:400,
        //enabled : false,
        returnKeyType:Titanium.UI.RETURNKEY_DONE,
        borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
    });
    popup_view.add(textSearch);
     
    ///////////////////////////////LOP///////////////////////////////////////////////////
    var lbLop = Ti.UI.createLabel({
		text:'Lớp',
		font:{
			fontSize:Ti.App.Properties.getString('fontsizeDefault')
		},
		
		height:'auto',
		left:10,
		top:55,
		color:Globals.color_TableView
		
	});
	popup_view.add(lbLop);
    
    pickerLop = Titanium.UI.createPicker({
		top:55, 
		height:50,
		right:10, 
		width:400,
		font:{
			fontSize:Ti.App.Properties.getString('fontsizeDefault')
		},
	});
	pickerLop.selectionIndicator=true;
	 
	var pickerLop_data = [
		Titanium.UI.createPickerRow({title:'Lớp 10',custom_item:'2'}),
		Titanium.UI.createPickerRow({title:'Lớp 11',custom_item:'4'}),
		Titanium.UI.createPickerRow({title:'Lớp 12',custom_item:'8'}),
		Titanium.UI.createPickerRow({title:'Luyện thi Lớp 10',custom_item:'1'}),
		Titanium.UI.createPickerRow({title:'Luyện thi Đại học',custom_item:'16'}),
		Titanium.UI.createPickerRow({title:'Luyện thi Đại học Vip',custom_item:'32'})
	];

	pickerLop.add(pickerLop_data);
	pickerLop.setSelectedRow(0, 4, true);
 
 	pickerLop.addEventListener("change", function(e){
		getDataMonHoc(e.row.custom_item,pickerMonHoc);
	});
	
	
      
	
 	///////////////////////////////MON HOC///////////////////////////////////////////////////
    var lbMonHoc = Ti.UI.createLabel({
		text:'Môn học',
		font:{
			fontSize:Ti.App.Properties.getString('fontsizeDefault')
		},
		
		height:'auto',
		left:10,
		top:110,
		color:Globals.color_TableView
		
	});
	popup_view.add(lbMonHoc);
    
    pickerMonHoc = Titanium.UI.createPicker({
		top:110,  
		height:50,
		//left:'105dp', 
		right:10, 
		width:400,
		font:{
			fontSize:Ti.App.Properties.getString('fontsizeDefault')
		},
	});
	pickerMonHoc.selectionIndicator=true;
	 
	
	var pickerMonHoc_data = [
		Titanium.UI.createPickerRow({title:'Toán',custom_item:'3',type:'16'}),
		Titanium.UI.createPickerRow({title:'Vật Lý',custom_item:'4',type:'16'}),
		Titanium.UI.createPickerRow({title:'Hóa Học',custom_item:'5',type:'16'}),
		Titanium.UI.createPickerRow({title:'Sinh Học',custom_item:'39',type:'16'}),
		Titanium.UI.createPickerRow({title:'Ngữ Văn',custom_item:'44',type:'16'}),
		Titanium.UI.createPickerRow({title:'Anh Văn',custom_item:'54',type:'16'}),
		Titanium.UI.createPickerRow({title:'Địa Lý',custom_item:'59',type:'16'})
	];

 	pickerMonHoc.add(pickerMonHoc_data);
	
 
 	///////////////////////////////BUTTON TIM KIEM ///////////////////////////////////////////////////
 	var btwidth=200;
	var btheight=45;
 	var btTimkiem = Titanium.UI.createButton({
		title:Globals.btSearch_popup,
		backgroundImage:Globals.backgroundImage_button,
		backgroundSelectedImage:Globals.backgroundSelectedImage,
		backgroundDisabledImage: Globals.backgroundDisabledImage,
		width:btwidth,
		height:btheight,
		left:50,
		bottom:'5dp',
		
		color:Globals.foreColor
	});
 
 	btTimkiem.addEventListener('click', function(e) {
 		//alert(pickerMonHoc.getSelectedRow(0).type)
        getListLecture(tableview,pickerMonHoc.getSelectedRow(0).custom_item,textSearch.value);
    });
 	///////////////////////////////BUTTON HUY BO ///////////////////////////////////////////////////
 	var btHuyBo= Titanium.UI.createButton({
		title:Globals.btCancel_popup,
		backgroundImage:Globals.backgroundImage_button,
		backgroundSelectedImage:Globals.backgroundSelectedImage,
		backgroundDisabledImage: Globals.backgroundDisabledImage,
		width:btwidth,
		height:btheight,
		right:50,
		bottom:'5dp',
		
		color:Globals.foreColor
	});
 	
 	btHuyBo.addEventListener('click', function(e) {
 			
        closePopUpView(popup_view);
       
    });
 	//////////////////////////////////////////////////////////////////////////
 	//////////////////////////////////////////////////////////////////////////
 	
 	win1.popUp = popup_view;
    popup_view._parent = win1;
   	popup_view.add(pickerMonHoc);
   	popup_view.add(pickerLop);
   	popup_view.add(btTimkiem);
 	popup_view.add(btHuyBo);
 	
 	
 	
 	
 	
    var popup_animation = Titanium.UI.createAnimation();
    // this will set it in to view
    popup_animation.top = 50;
    // ... in half a second
    popup_animation.duration = 500;
 
   

 
    // add it when you need to (it will still be 'off screen')
    win1.add(popup_view);
 
    // then when you need it to 'appear' / pop up ... use:
    popup_view.animate(popup_animation);
}


function getDataMonHoc(idLop,pickerMonHoc) {
   var row = null;
	var data = [];
	var param = {        
	  "typeId" : idLop
	};
	var xhr = Titanium.Network.createHTTPClient({
   
		   onload : function(e) 
		   {
		       Titanium.API.info(this.responseText);
		       
		       //do whatever you want to do with your response
		   
		
				 json = this.responseText;
				 json=json.replace('<string xmlns="http://tempuri.org/">', '');
				 json=json.replace('<?xml version="1.0" encoding="utf-8"?>', '');
				 json=json.replace('</string>', '');
				//  alert(json)
				 response = JSON.parse(json);
		
				////////////////////CLEAR DATA////////////////////////////
				if(pickerMonHoc.columns[0]) {
				    var _col = pickerMonHoc.columns[0];
				        var len = _col.rowCount;
				        for(var x = len-1; x >= 0; x-- ){
				                var _row = _col.rows[x];
				                _col.removeRow(_row);
				        }
				}
				////////////////////CLEAR DATA////////////////////////////
				
			 	for (i = 0; i < response.OBJECT.length; i++) {
						olecture = response.OBJECT[i];
						data[i]=	Titanium.UI.createPickerRow({title:olecture.Name,custom_item:olecture.Id,type:olecture.Type});
				}
				pickerMonHoc.add(data);
		   }, 
   
		   onerror : function(e) {
		   	   //activityIndicator.hide();
		       require('Dialog').showError();
		   },
		   timeout : Globals.timeout //5000
    
	});
	xhr.open("POST",Globals.serviceurl_GetListMonHoc);
	xhr.send(param);
	//return data;
}

  
function closePopUpView(_view) {
    var popup_animation = Titanium.UI.createAnimation();
    if (_view != null) {
    	
        // this will set it in to view
        popup_animation.bottom = -240;
        // ... in half a second
        popup_animation.duration = 500;
      //  _view.animate(popup_animation, function(){
            _view._parent.remove(_view);
            
     //   });
 		
		 
 		/*
 		popup_animation.bottom = -240;
        // ... in half a second
        popup_animation.duration = 500;
 		 _view._parent.remove(_view);
 		 
 		
 		
 		
 		*/
	    
	    
    }
}

       
     return win;
       
    };
})();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  